package com.neu.cpsp.controller;

import com.neu.cpsp.pojo.RegulationType;
import com.neu.cpsp.service.RegulationTypeService;
import com.neu.cpsp.util.result.Result;
import com.neu.cpsp.util.result.ResultGenerator;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

/**
 * @author neu.xiaojiacheng
 * @version 1.0
 * @date 2022/2/27 14:36
 * 规则种类Controller
 */

@CrossOrigin
@RestController
@RequestMapping("/regulationtype")
public class RegulationTypeController {

    @Autowired
    private RegulationTypeService regulationTypeService;

    @ApiOperation("添加记录")
    @RequestMapping(value = "/addForm", method = RequestMethod.POST)
    @ResponseBody
    public Result addForm(@RequestParam String name,
                          @RequestParam String checkSide,
                          @RequestParam Boolean deleteFlag){
        RegulationType record = new RegulationType();
        record.setName(name);
        record.setCheckSide(checkSide);
        if(deleteFlag==null){
            record.setDeleteFlag(false);
            //false为在使用
        }else{
            record.setDeleteFlag(deleteFlag);
        }
        if (regulationTypeService.submitAllInfo(record))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    /*返回给前端需要的规则种类*/
    //@PreAuthorize(hasPermi = "regulationtype:select")
    @ApiOperation("查找")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@RequestParam Integer pageSize,
                         @RequestParam Integer currentPage,
                         @RequestParam String name) {
        if (name == null)
            return ResultGenerator.genSuccessResult(regulationTypeService.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(regulationTypeService.getByName(pageSize, currentPage, name));
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(regulationTypeService.delete(id));
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestParam Integer id,
                         @RequestParam String name,
                         @RequestParam String checkSide,
                         @RequestParam Boolean deleteFlag) {

        RegulationType record = new RegulationType();
        record.setId(id);
        record.setName(name);
        record.setCheckSide(checkSide);
        record.setDeleteFlag(deleteFlag);
        return ResultGenerator.genSuccessResult(regulationTypeService.update(record));
    }


    @ApiOperation("联合Regulation_type以及Regulation_detail表查询")
    @PostMapping(value = "/selectTypeAndDetail")
    @ResponseBody
    public Result selectTypeAndDetail(RegulationType regulationType) {
        return ResultGenerator.genSuccessResult(regulationTypeService.getTypeAndDetail(regulationType));
    }
}
